#encoding:utf-8
from celery import Celery
from flask_mail import Message
from exts import mail
#新创建一个app，避免循环引用
from flask import Flask
# from utils.Send_sms import send_sms
import config

app = Flask(__name__)
app.config.from_object(config)
#邮件初始化app,读取config相关配置
mail.init_app(app)

def make_celery(app):
    celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)
    TaskBase = celery.Task
    class ContextTask(TaskBase):
        abstract = True
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return TaskBase.__call__(self, *args, **kwargs)
    celery.Task = ContextTask
    return celery

celery = make_celery(app)

#发送邮件
@celery.task
def send_mail(subject,recipients,body):
    message = Message(subject=subject,recipients=recipients,body=body)
    mail.send(message)

# #发送短信验证码
# @celery.task
# def send_sms_captcha(telephone,captcha):
#     send_sms(telephone,text=captcha)
#
#



